﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows;
using DataProcessing;
namespace Gene.Render
{
    /// <summary>
    /// a text plane for XZ plane
    /// </summary>
    class TextPlane
    {
 
        public Point LeftUp
        {
            get;
            set;
        }
        public double Width
        {
            get;
            set;
        }

        public double Height
        {
            get;
            set;
        }

        public GeometryModel3D GeometryModel
        {
            get;set;
        }
        MeshGeometry3D _MeshGeometry3D; 

        public double Y
        {
            get;
            set;

        }
        private Brush _drawer;

        public String Text
        {
            get;
            set;
        }

   

        public TextPlane(Point leftUp,double width,double height,double y,String text)
        {
            LeftUp = leftUp;
            Width = width;
            Height = height;
            _MeshGeometry3D = new MeshGeometry3D();
            GeometryModel = new GeometryModel3D();
            Y = y;
            Text = text;
            Build();
        }

       public void Build()
        {
            // Create a collection of vertex positions for the MeshGeometry3D. 

            Point3DCollection myPositionCollection = new Point3DCollection();
            myPositionCollection.Add(new Point3D(LeftUp.X, Y, LeftUp.Y));
            myPositionCollection.Add(new Point3D(LeftUp.X+Width, Y, LeftUp.Y));
            myPositionCollection.Add(new Point3D(LeftUp.X+Width, Y, LeftUp.Y+Height));
            myPositionCollection.Add(new Point3D(LeftUp.X, Y, LeftUp.Y + Height));


            _MeshGeometry3D.Positions = myPositionCollection;



            // Create a collection of triangle indices for the MeshGeometry3D.
            Int32Collection myTriangleIndicesCollection = new Int32Collection();
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(2);

            myTriangleIndicesCollection.Add(2);
            myTriangleIndicesCollection.Add(3);
            myTriangleIndicesCollection.Add(0);

            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(3);
            myTriangleIndicesCollection.Add(2);

            myTriangleIndicesCollection.Add(2);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(0);
            _MeshGeometry3D.TriangleIndices = myTriangleIndicesCollection;

            // Apply the mesh to the geometry model.
            GeometryModel.Geometry = _MeshGeometry3D;

            // Create a collection of texture coordinates for the MeshGeometry3D.
            PointCollection myTextureCoordinatesCollection = new PointCollection();
            myTextureCoordinatesCollection.Add(new Point(0, 0));
            myTextureCoordinatesCollection.Add(new Point(1, 0));
            myTextureCoordinatesCollection.Add(new Point(1, 1));
            myTextureCoordinatesCollection.Add(new Point(0, 1));

            _MeshGeometry3D.TextureCoordinates = myTextureCoordinatesCollection;


            RenderTargetBitmap targetImage = new RenderTargetBitmap(60,
                                                                    30,
                                                                    96,
                                                                    96,
                                                                    PixelFormats.Pbgra32);
            Draw(targetImage);
            _drawer = new ImageBrush(targetImage);
            // Define material and apply to the mesh geometries.
            EmissiveMaterial myMaterial = new EmissiveMaterial(_drawer);
            GeometryModel.Material = myMaterial;
        }
       public void AddTo(Model3DGroup modelGroup)
       {
           Build();
           modelGroup.Children.Add(this.GeometryModel);
       }
        void Draw(RenderTargetBitmap target)
        {
             FormattedText formattedText = new FormattedText(
                        Text,
                        System.Globalization.CultureInfo.GetCultureInfo("en-us"),
                        FlowDirection.LeftToRight,
                        new Typeface("Verdana"),
                        15,
                        Brushes.LightGreen);
            
            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {
                SolidColorBrush brush = new SolidColorBrush(Colors.LightGreen);
                drawingContext.DrawText(formattedText,new  Point(1,1));

            }
            target.Render(drawingVisual);

   
        }

           

    }
}
